//
//  CacheManager.swift
//  BLH
//
//  Created by zhangshumeng on 2021/8/10.
//

import UIKit

class CacheManager<T: Codable>: NSObject {

    private var storage: Storage<T>?
    
    init(name: String = "Cache") {
        let diskConfig = DiskConfig(name: name, directory: URL(fileURLWithPath: NSHomeDirectory() + "/Documents/Cache/"))
        let memoryConfig = MemoryConfig(expiry: .never)
        do {
            storage = try Storage(diskConfig: diskConfig, memoryConfig: memoryConfig, transformer: TransformerFactory.forCodable(ofType: T.self))
        } catch {
            debugPrint("CacheManager init storage error: \(error)")
        }
    }
    
    func setObject(_ object: T, forKey: String) {
        do {
            try storage?.setObject(object, forKey: forKey)
        } catch {
            debugPrint("CacheManager save obiect error: \(error)")
        }
    }

    func object(forKey key: String) -> T? {
        do {
            return try storage?.object(forKey: key) ?? nil
        } catch {
            debugPrint("CacheManager get obiect error: \(error)")
            return nil
        }
    }

    func removeObiect(forKey key: String) {
        do {
            try storage?.removeObject(forKey: key)
        } catch {
            debugPrint("CacheManager remove obiect error: \(error)")
        }
    }

    func removeAll() {
        do {
            try storage?.removeAll()
        } catch {
            debugPrint("CacheManager remove all obiect error: \(error)")
        }
    }
}

// MAKE: 绘本
class BookCacheManager {
    
    static let share = BookCacheManager()
    private let cacheManger = CacheManager<RecChildDataModel>(name: "Book")
    private let path = NSHomeDirectory() + "/Documents/Cache/Book"
    
    /// 保存绘本
    func setBook(_ model: RecChildDataModel, forKey: String) {
        cacheManger.setObject(model, forKey: forKey.MD5String)
    }
    
    /// 获取绘本
    func object(forKey key: String) -> RecChildDataModel? {
        cacheManger.object(forKey: key)
    }
    
    /// 移除绘本
    func removeObiect(forKey key: String) {
        cacheManger.removeObiect(forKey: key.MD5String)
    }
    
    /// 移除所有绘本
    func removeAll() {
        cacheManger.removeAll()
    }
    
    /// 获取所有绘本的缓存
    func getAllBooks() -> [RecChildDataModel] {
        do {
            let files = try FileManager.default.contentsOfDirectory(atPath: path)
            var books = [RecChildDataModel]()
            for file in files {
                if let book = object(forKey: file) {
                    books.append(book)
                }
            }
            return books
        } catch {
            debugPrint(error.localizedDescription)
            return []
        }
    }
}

// MAKE: 视频缓存
class VideoCacheManager {
    static let share = VideoCacheManager()
    private let cacheManger = CacheManager<RecChildDataModel>(name: "Video")
    private let path = NSHomeDirectory() + "/Documents/Cache/Video"
    
    /// 保存视频
    func setVideo(_ model: RecChildDataModel, forKey: String) {
        cacheManger.setObject(model, forKey: forKey.MD5String)
    }
    
    /// 获取视频
    func object(forKey key: String) -> RecChildDataModel? {
        cacheManger.object(forKey: key)
    }
    
    /// 移除视频
    func removeObiect(forKey key: String) {
        cacheManger.removeObiect(forKey: key.MD5String)
    }
    
    /// 移除所有视频
    func removeAll() {
        cacheManger.removeAll()
    }
    
    /// 获取所有视频的缓存
    func getAllVideos() -> [RecChildDataModel] {
        do {
            let files = try FileManager.default.contentsOfDirectory(atPath: path)
            var videos = [RecChildDataModel]()
            for file in files {
                if let video = object(forKey: file) {
                    videos.append(video)
                }
            }
            return videos
        } catch {
            debugPrint(error.localizedDescription)
            return []
        }
    }
}
